package com.test.demo.mydubbo.framework.protocol;

import com.test.demo.mydubbo.framework.protocol.dubbo.NettyClient;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyFactory<T> {

    public static <T> T getProxy(final Class interfaceClass){

        return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class[]{interfaceClass}, new InvocationHandler() {
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                DataFormater dataFormater=new DataFormater();
                dataFormater.setInterfaceName(interfaceClass.getName());
                dataFormater.setParameterTypes(method.getParameterTypes());
                dataFormater.setMethodName(method.getName());
                dataFormater.setArgs(args);
                NettyClient nettyClient= new NettyClient();
                return nettyClient.send(new URL("localhost",9000),dataFormater);
            }
        });
    }
}
